#!/usr/bin/perl -w
#-----------------------------------------------------------+
#                                                           |
# con_mcl2na.pl - Convert mcl output to NA file             |
#                                                           |
#-----------------------------------------------------------+
#                                                           |
# J Estill                                                  |
# 08/24/2007                                                |
#                                                           |
# Convert MCL output to a Node Attributes file              |
# suitable for use in Cytoscape. This will be used as part  |
# of the RepMiner suite of programs to visualize MCL        |
# defined clusters onto visualizations of the network.      |
#                                                           |
#-----------------------------------------------------------+

#-----------------------------+
# INCLUDES                    |
#-----------------------------+ 
use strict;
use Getopt::Long;

#-----------------------------+
# VARIABLES                   |
#-----------------------------+

# CONSTANTS
my $VERSION = "1.0";

# SET VARIABLES SCOPE
my $infile;
my $outfile;
my $suffix;                    # Name to append to the end of the NA file
<<<<<<< .mine
                               # Allows for parsing multiple MCL Clust runs
=======
                               # Allow for parsing multiple MCL Clust runs
>>>>>>> .r70

# INITIALIZE COUNTERS
my $clust_num = 0;

# BOOLEANS
my $show_help = 0;             # Display help
my $quiet = 0;                 # Run the program in quiet mode
                               # will not prompt for command line options
my $show_node_id = 0;          # Include the database node_id in the output
my $show_man = 0;              # Show the man page via perldoc
my $show_usage = 0;            # Show the basic usage for the program
my $show_version = 0;          # Show the program version
my $verbose = 0;    

#-----------------------------+
# COMMAND LINE OPTIONS        |
#-----------------------------+
my $ok = GetOptions(# REQUIRED ARGUMENTS
                    "i|infile=s" => \$infile,
                    "o|outfile=s"=> \$outfile,
		    # ADDITIONAL OPTIONS
		    "s|suffix=s" => \$suffix,
		    # BOOLEANS
		    "q|quiet"    => \$quiet,
                    "verbose"    => \$verbose,
		    "version"    => \$show_version,
		    "man"        => \$show_man,
		    "usage"      => \$show_usage,
		    "h|help"     => \$show_help,);

print "con_mcl2na.pl started\n" if $verbose;


#-----------------------------+
# SHOW REQUESTED HELP         |
#-----------------------------+

if ($show_usage) {
    print_help("");
}

if ($show_help || (!$ok) ) {
    print_help("full");
}

if ($show_version) {
    print "\n$0:\nVersion: $VERSION\n\n";
    exit;
}

if ($show_man) {
    # User perldoc to generate the man documentation.
    system("perldoc $0");
    exit($ok ? 0 : 2);
}

open (IN, "<".$infile) ||
    die "Could not open input file $infile\n";

# This is the Node Attribute *.NA file
# Write the NA header file, add suffix is requested

open (OUT, ">".$outfile) ||
    die "Could not open outfile $outfile";
if ($suffix) {
    print OUT "MCL_Cluster_$suffix\n";
}
else {
    print OUT "MCL_CLUSTERS\n";
}

while (<IN>) {

    $clust_num++;
    chomp;
    print "Processing cluster: $clust_num\n" if $verbose;

    # Currently just splitting on the whitespace
    # splitting on tab would be better

    my @seq_ids = split;
    
    # For each seq record print its cluster id
    # and the name
    for my $seq_id (@seq_ids) {
	# TO DO: split again by pipe to get the number to 
	#        use to map node attributes in cytoscape
	my ($id_num,$rest) = split(/\|/,$seq_id);

	print OUT "$id_num=MCL_Clust_$clust_num\n";

	print "CLUST:$clust_num:\t$seq_id" if $verbose;
    }
    
}

exit;

#-----------------------------------------------------------+
# SUBFUNCTIONS                                              |
#-----------------------------------------------------------+

sub print_help {

    # Print requested help or exit.
    # Options are to just print the full 
    my ($opt) = @_;

    my $usage = "USAGE:\n". 
	"  con_mcl2na.pl -i infile.clus -o outfile.NA [-s suffix]";
    my $args =       "REQUIRED ARGUMENTS:\n".
      "  --infile     # Input file to translate to NA file\n".
      "  --outfile    # Path to the output file\n".
      "ADDITIONAL OPTIONS:\n".
      "  --suffix     # Suffix to add to the NA file header\n".
      "ADDITIONAL INFORMATION:\n".
      "  --version    # Show the program version\n".     
      "  --usage      # Show program usage\n".
      "  --help       # Show a short help message\n".
      "  --man        # Show full program manual\n";
    if ($opt =~ "full") {
	print "\n$usage\n\n";
	print "$args\n\n";
    }
    else {
	print "\n$usage\n\n";
    }
    
    exit;
}


=head1 NAME 

con_mcl2na.pl - Convert MCL output to NA file

=head1 VERSION

This documentation refers to phyinit version 1.0.

=head1 SYNOPSIS
    
  USAGE: con_mcl2na.pl -i infile.clus -o outfile.clus [-s suffix]

    REQUIRED ARGUMENTS:
      --infile     # Input file to process
      --outfile    # Output file produced
    ADDITIONAL OPTIONS:
      --suffix     # Suffix to add to the NA file header
    ADDITIONAL INFORMATION:
      --version    # Show the program version
      --usage      # Show program usage
      --help       # Show a short help message
      --man        # Show full program manual

=head1 DESCRIPTION

Convert MCL output to a Node Attributes file
suitable for use in Cytoscape. This will be used as part
of the RepMiner suite of programs to visualize MCL
defined clusters onto visualizations of the network.

=head1 COMMAND LINE OPTIONS

=head2 Required Arguments

=over 2

-i, --infile

Path to input file to process. This should be a tab delimited text file 
produced with mcxdeblast of the MCL package.

-o, --outfile

Path to the output file created. This should include the NA extension.

=back

=head2 Additional Options

=over 2

-s, --suffix

Optional suffix to be added to the header line of the NA file.

=back

=head2 Additional Information

=over 2

=item --version

Show the program version.   

=item --usage      

Show program usage statement.

=item --help

Show a short help message.

=item --man

Show the full program manual.

=back

=back

=head1 EXAMPLES

B<Expected output for MCL>

This program expects MCL output generated by mcxdeblast such as:

    mcxdeblast --m9 --abc-out=- rice_stow_e20.blo | mcl - --abc -o xyz.clus
 
B<Typical run>

This above results would be parsed as

    con_mcl2na.pl -i xyz.clus  -i my_outfile.NA

The resulting my_outfile.NA file would a text file in the form

    MCL_CLUSTERS
    5947=MCL_Clust_1
    5784=MCL_Clust_1
    1779=MCL_Clust_1
    1549=MCL_Clust_1
    1286=MCL_Clust_1
    .....

B<Adding a suffix to the NA file header>

The additon of a suffix can be used to distinguish between MCL parameter settings

    con_mcl2na.pl -i xyz.clus -o my_out_setone.NA -s setone

This will result in an ouput file name my_out_setone.NA. The header for the
NA file will contain the suffix. 

    MCL_CLUSTERS_setone
    5947=MCL_Clust_1
    5784=MCL_Clust_1
    1779=MCL_Clust_1
    1549=MCL_Clust_1
    1286=MCL_Clust_1
    .....

Note that the outputfile name will not have the suffix, you simply just
name the outifle with the -o option.

B<Long option names>

The following uses the long option name format

    con_mcl2na.pl --infile xyz.clus --outfile my_out_setone.NA --suffix setone

=cut
